-- UISunFlower
-- Created by zouyb Dec/29/2015
-- 太阳花种植主界面

UISunFlower = class("UISunFlower", function()
    return cc.Layer:create();
end);

function UISunFlower.create(pos)
    return UISunFlower.new(pos);
end

-- 模型缩放比例
local MODEL_SCALE = 0.65;

-- 光效缩放比例
local EFFECT_SCALE = 1.5;

function UISunFlower:ctor(pos)
    -- 初始化
    self:setName("UISunFlower");
    local node = cc.CSLoader:createNode("layout/bine/SunFlower.csb");
    self:addChild(node);
    self.node = node;

    self.pos = pos;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 获取太阳花对象
    self.bine = ME.user.bine;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UISunFlower:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UISunFlower:registerEventCallback()

    -- 关注制造阳光事件
    EventMgr.register("UISunFlower", event.MAKE_SUNSHINE, function()
        alert(getLocStr("make_sunshine_succ"));

        -- 太阳光飞入包裹效果
        local classId = 7258;
        local grid = UIDungeonMgr.getCurLevel():getAllGrids()[self.pos];
        if grid then
            local iconPath = getItemIconPath(ItemM.query(classId, "icon"));
            pickupEquipEffect(grid, UIDungeonMgr.getCurLevel():getEquipNode(), iconPath, classId, 1);
        end

        -- 关闭界面
        UIMgr.getCurrentScene():removeFormByName("UISunFlower");
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UISunFlower 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UISunFlower");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UISunFlower:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UISunFlower");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册按钮点击事件
    local btnMake = findChildByName(self.node, "CT/btn_make");
    local function onUpgradeClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            -- 尝试制造太阳光
			self:tryMakeSunshine();
        end
    end
    btnMake:addTouchEventListener(onUpgradeClick);
    TextStyleM.setTextStyle(btnMake, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnMake:setTitleText(getLocStr("btn_text_make_sunshine"));
end

-- 重绘
function UISunFlower:redraw()
    -- 显示标题
    local name = BineM.query(PLANT_CLASS_SUN_FLOWER, "name");
    local titleLabel = findChildByName(self.node, "CT/bg/bg/title_label");
    titleLabel:setString(name);
    TextStyleM.setArtWordTitleStyle(titleLabel);

	-- 显示太阳花名字和介绍
    local nameLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    nameLabel:setString(name);

    local descLabelKerning = -1;
    if getLang() == "zh" or getLang() == "tw" then
        descLabelKerning = 1;
    end
    local descLabel = findChildByName(self.node, "CT/tip");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, descLabelKerning);
    descLabel:setString(BineM.query(PLANT_CLASS_SUN_FLOWER, "desc"));

    -- 显示太阳花模型
    self:redrawModel();

    -- 显示太阳花属性
    self:redrawAttrib();

    -- 显示消耗
    self:redrawCost();
end

-- 显示太阳花模型
function UISunFlower:redrawModel()
    local leftPart = findChildByName(self.node, "CT/left_part");

    -- 绘制模型
    local modelNode = findChildByName(leftPart, "model_node");
    modelNode:removeAllChildren();

    local bineDisplay = self:createBineDisplay();
    modelNode:addChild(bineDisplay);
end

-- 显示太阳花属性
function UISunFlower:redrawAttrib()
    local str = "--";

    local valueLabel, nameLabel;
    local rightPart = findChildByName(self.node, "CT/bg/right_part");

    -- 攻击
    nameLabel = findChildByName(rightPart, "attack_node/name");
    valueLabel= findChildByName(rightPart, "attack_node/value");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    nameLabel:setString(getLocStr("attack"));
    valueLabel:setString(str);

    -- 生命
    local nameLabel= findChildByName(rightPart, "hp_node/name");
    local valueLabel= findChildByName(rightPart, "hp_node/value");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    nameLabel:setString(getLocStr("hp"));
    valueLabel:setString(str);

    -- 魔力
    local nameLabel= findChildByName(rightPart, "dodge_node/name");
    local valueLabel= findChildByName(rightPart, "dodge_node/value");
    local iconImg = findChildByName(rightPart, "dodge_node/icon");
    iconImg:loadTexture(getSmallAttribIconPath("magic"));
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    nameLabel:setString(getLocStr("magic"));
    valueLabel:setString(str);

    -- 魔法
    local nameLabel= findChildByName(rightPart, "accuracy_node/name");
    local valueLabel= findChildByName(rightPart, "accuracy_node/value");
    local iconImg = findChildByName(rightPart, "accuracy_node/icon");
    iconImg:loadTexture(getSmallAttribIconPath("mp"));
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_BEIGE, false, -1);
    nameLabel:setString(getLocStr("mp"));
    valueLabel:setString(str);
end

-- 显示消耗
function UISunFlower:redrawCost()
    -- 升级消耗
    local cost = BineM.query(PLANT_CLASS_SUN_FLOWER, "material");
    local classId = table.keys(cost)[1];
    local amount  = table.values(cost)[1];
    local curAmount = ItemM.getAmount(ME.user, classId);

    -- 消耗不足时，红色显示
    local priceLabel = findChildByName(self.node, "CT/price_node/price");
    local iconImg = findChildByName(self.node, "CT/price_node/icon");
    local icon = ItemM.query(classId, "icon");
    iconImg:loadTexture(getItemIconPath(icon));

    if ItemM.getAmount(ME.user, classId) < amount then
        TextStyleM.setTextStyle(priceLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE, false);
    else
        TextStyleM.setTextStyle(priceLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE, false);
    end
    priceLabel:setString(curAmount .. "/" .. amount);

    iconImg:setScale(0.6);
    iconImg:setPositionY(0);

    -- 记录消耗物品ID
    self.materialId = classId;
end

-- 创建模型
function UISunFlower:createBineDisplay()
    -- 创建模型
    local bine = ME.user.bine;
    local classId = bine.classId;
    local bineDisplay = BineDisplay.create(classId);

    -- 设置缩放比例
    bineDisplay:setScale(MODEL_SCALE);

    -- 设置偏移量
    local offsetX = 0;
    local offsetY = 20;

    bineDisplay:setPosition(tonumber(offsetX), tonumber(offsetY));
    return bineDisplay;
end

-- 尝试制造太阳光
function UISunFlower:tryMakeSunshine()
    if not BineM.canMakeSunshine(self.pos) then
        local name = ItemM.query(self.materialId, "name");
        alert(string.format(getLocStr("item_not_enough2"), name));
        return;
    end
    DungeonActionM.go("make_sunshine", self.pos);
end


